import 'dart:convert';

import 'package:calf_account/api/pass_api.dart';
import 'package:calf_account/model/password.dart';
import 'package:calf_account/utils/random_util.dart';
import 'package:flutter/cupertino.dart';
import 'package:get/get.dart';

class EditPassLogic extends GetxController {
  final FocusNode passFocusNode = FocusNode();
  final TextEditingController titleController = TextEditingController();
  final TextEditingController userController = TextEditingController();
  final TextEditingController passController = TextEditingController();
  final TextEditingController urlController = TextEditingController();
  final TextEditingController noteController = TextEditingController();

  double passLength = 6.0;
  bool openPass = false;
  bool passNumber = true;
  bool passLetter = true;
  bool passSymbol = true;

  @override
  void onInit() {
    passFocusNode.addListener(() {
      //改变密码配置窗口的显示/影藏
      changeOpenPass(passFocusNode.hasFocus);
    });
    super.onInit();
  }

  @override
  onClose() {
    passFocusNode.dispose();
    titleController.dispose();
    userController.dispose();
    passController.dispose();
    urlController.dispose();
    noteController.dispose();
    super.onClose();
  }

  ///页面渲染完成后加载数据
  @override
  void onReady() {}

  ///更改密码配置窗口显示
  void randomPassword() {
    passController.text = RandomUtil().randomPassword(passLength.toInt(),
        hasNumber: passNumber, hasLetter: passLetter, hasSymbol: passSymbol);
  }

  ///更改密码配置窗口显示
  void changeOpenPass(bool val) {
    openPass = val;
    update(["passConfig"]);
  }

  ///更改密码长度slider
  void changePassLength(double val) {
    passLength = val;
    update(["passSlider"]);
  }

  ///更改数字开关
  void changePassNumber(bool val) {
    passNumber = val;
    update(["passNumberSwitch"]);
  }

  ///更改字母开关
  void changePassLetter(bool val) {
    passLetter = val;
    update(["passLetterSwitch"]);
  }

  ///更改符号开关
  void changePassSymbol(bool val) {
    passSymbol = val;
    update(["passSymbolSwitch"]);
  }

  ///提交
  void submitForm() {
    Password ps = Password();

    PasswordApi.localSavePassword(ps);
  }
}
